package cn.itsource.pethome.system.menu.controller;

import cn.itsource.pethome.annotation.PreAuthorize;
import cn.itsource.pethome.system.menu.service.IMenuService;
import cn.itsource.pethome.system.menu.domain.Menu;
import cn.itsource.pethome.system.menu.query.MenuQuery;
import cn.itsource.pethome.utils.AjaxResult;
import cn.itsource.pethome.utils.BusinessException;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    public IMenuService menuService;


    /**
     * 保存和修改公用的
     *
     * @param menu 传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    @PreAuthorize(name = "菜单的保存",value = "menu:save")
    public AjaxResult addOrUpdate(@RequestBody Menu menu) {
        try {
            if (menu.getId() != null)
                menuService.update(menu);
            else
                menuService.add(menu);
            return AjaxResult.success();
        } catch (BusinessException e){
            return AjaxResult.error().setMsg(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }
    }

    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    @PreAuthorize(name = "菜单的删除",value = "menu:delete")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            menuService.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }
    }

    //获取用户
    @GetMapping("/{id}")
    @PreAuthorize(name = "菜单查询一个",value = "menu:getOne")
    public Menu get(@PathVariable("id") Long id) {
        return menuService.findOne(id);
    }


    /**
     * 查看所有的员工信息
     *
     * @return
     */
    @GetMapping()
    @PreAuthorize(name = "菜单查询全部",value = "menu:findAll")
    public List<Menu> list() {

        return menuService.findAll();
    }


    @PatchMapping
    @ApiOperation(value = "根据id批量删除菜单")
    @PreAuthorize(name = "菜单批量删除",value = "menu:batchDel")
    public AjaxResult batchDel(@RequestBody List<Long> ids) {
        try {
            menuService.batchDel(ids);
            return AjaxResult.success().setMsg("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error().setMsg("删除失败");
    }

    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/list")
    @PreAuthorize(name = "菜单分页查询",value = "menu:list")
    public AjaxResult json(@RequestBody MenuQuery query) {
        try {
            List<Menu> byQuery = menuService.findByQuery(query);
            Long count = menuService.findByQueryCount(query);
            return AjaxResult.success().setData(byQuery).setTotal(count);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }

    @GetMapping("/first")
    public AjaxResult first() {
        try {
            List<Menu> byQuery = menuService.findFirst();
            return AjaxResult.success().setData(byQuery);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }

    @GetMapping("/tree")
    public List<Menu> tree() {
       return menuService.tree();
    }


}
